package org.springframework.beans.factory.config.custom;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

public class PropertyPlaceholderConfigurer extends org.springframework.beans.factory.config.PropertyPlaceholderConfigurer {

    private static Logger log = Logger.getLogger(PropertyPlaceholderConfigurer.class.getName());

    private static LinkedHashMap<String, String> propertiesMap;
    // Default as in PropertyPlaceholderConfigurer
    private int springSystemPropertiesMode = SYSTEM_PROPERTIES_MODE_FALLBACK;

    @Override
    public void setSystemPropertiesMode(int systemPropertiesMode) {
	super.setSystemPropertiesMode(systemPropertiesMode);
	springSystemPropertiesMode = systemPropertiesMode;
    }

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) throws BeansException {
	super.processProperties(beanFactory, props);

	propertiesMap = new LinkedHashMap<String, String>();
	for (Object key : props.keySet()) {
	    String keyStr = key.toString();
	    String valueStr = resolvePlaceholder(keyStr, props, springSystemPropertiesMode);
	    propertiesMap.put(keyStr, valueStr);
	}
    }

    public static String getProperty(String key) {
	try {
	    return propertiesMap.get(key);
	} catch (NullPointerException e) {
	    log.error(e, e.getCause());
	    return null;
	}
    }

    public static Map<String, String> getAllProperties() {
	return propertiesMap;
    }

}
